# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
import requests
from config import url,proxy,is_window,sleep_time
from time import sleep

# 获取浏览器对象
def get_browser():
   option = ChromeOptions()
   #  关闭自动控制  
   option.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
   option.add_experimental_option('useAutomationExtension', False)
   #  代理IP
   BG_proxy = requests.get(proxy).text
   option.add_argument("--proxy-server=" + BG_proxy)
   #  是否显示浏览器
   if not is_window:
     option.add_argument('--headless')
     option.add_argument('--disable-gpu')
   #  干掉全局变量
   browser = webdriver.Chrome(options=option)
   browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
         'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
         })
   return browser

def fn(count):
  web = get_browser()
  web.get('http://icanhazip.com')
  sleep(3)
  web.quit()
  count += 1
  if count < 4:
    fn(count)

fn(0)